﻿#include "framework.h"
#include "mkiso.h"
#include <stdio.h>
#include <CommDlg.h>
#include <shlobj.h>
#include <Shlwapi.h>

#pragma comment(lib,"shlwapi.lib")

static char m_exePath[1024];
static char m_dirPath[1024];
static char m_isoPath[1024];
static char m_label[1024];

BOOL SelectFolder(HWND hwnd, char *output)
{
	BROWSEINFO          sInfo;
	LPITEMIDLIST        lpidlBrowse = NULL;

	::ZeroMemory(&sInfo, sizeof(BROWSEINFO));
	sInfo.pidlRoot = 0;
	sInfo.pszDisplayName = output;
	sInfo.lpszTitle = _T("选择文件夹:");
	sInfo.ulFlags = BIF_USENEWUI;
	sInfo.lpfn = NULL;
	lpidlBrowse = ::SHBrowseForFolder(&sInfo);
	if (lpidlBrowse != NULL)
	{
		if (::SHGetPathFromIDList(lpidlBrowse, output))
		{
			return TRUE;
		}
	}
	return FALSE;
}

BOOL SaveFileBox(HWND hWnd, char* pzCaption, char* pzFilter, char* pzFileName)
{
	OPENFILENAME ofn;
	ZeroMemory(&ofn, sizeof(ofn));
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hInstance = NULL;
	ofn.nFilterIndex = 0;
	ofn.lpstrFile = NULL;          // Set in Open and Close functions
	ofn.nMaxFile = 1024;
	ofn.Flags = 0;             // Set in Open and Close functions

	ofn.hwndOwner = hWnd;
	ofn.lpstrTitle = pzCaption;
	ofn.lpstrFilter = pzFilter;
	ofn.lpstrFile = pzFileName;
	ofn.Flags = OFN_OVERWRITEPROMPT;

	ofn.lpstrFile[0] = 0;
	return GetSaveFileName(&ofn);
}

BOOL CheckParams(HWND hwnd)
{
	GetDlgItemTextA(hwnd, IDC_EDIT1, m_dirPath, sizeof(m_dirPath) - 1);
	if (!PathIsDirectory(m_dirPath))
	{
		MessageBoxA(hwnd, "输入目录无效,请重新选择!", "错误", MB_ICONWARNING);
		return FALSE;
	}
	GetDlgItemTextA(hwnd, IDC_EDIT2, m_isoPath, sizeof(m_isoPath) - 1);
	if (strnlen_s(m_isoPath, sizeof(m_isoPath)) == 0)
	{
		MessageBoxA(hwnd, "输出文件路径无效,请重新选择!", "错误", MB_ICONWARNING);
		return FALSE;
	}
	char tmp[1024];
	strcpy_s(tmp, m_isoPath);
	PathRemoveFileSpecA(tmp);
	if (!PathIsDirectory(tmp))
	{
		MessageBoxA(hwnd, "输出文件路径无效,请重新选择!", "错误", MB_ICONWARNING);
		return FALSE;
	}
	GetDlgItemTextA(hwnd, IDC_EDIT3, m_label, 1024);
	return TRUE;
}

//CDIMAGE.EXE -m -j2 <dir> <iso>
BOOL CreateISO(HWND hwnd)
{
	STARTUPINFOA si;
	PROCESS_INFORMATION pi;
	char cmdline[1024];
	GetStartupInfoA(&si);
	snprintf(cmdline, 1024, "oscdimg.exe %s \"%s\" \"%s\"", m_label, m_dirPath, m_isoPath);
	return CreateProcessA(m_exePath, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
}

static void OnInitDialog(HWND hwnd)
{
	LPCSTR help = "本工具为oscdimg设计的图形界面.\r\n"
		"oscdimg常用参数:\r\n"
		" -o\t优化文件体积\r\n"
		" -h\t包含隐藏文件\r\n"
		" -m\t忽略文件大小限制\r\n"
		" -lXX\t设置标卷名为XX\r\n"
		" -n\t允许长文件名(ISO9660文件系统)\r\n"
		" -d\t允许小写文件名(ISO9660文件系统)\r\n"
		" -j1\t兼容ISO9660文件系统和Joliet文件系统\r\n"
		" -j2\t仅使用Joliet系统\r\n";
	SetDlgItemTextA(hwnd, IDC_EDIT5, help);
	SetDlgItemTextA(hwnd, IDC_EDIT3, "-o -m -j2 -lNONAME");
};

static void OnCommand(HWND hwnd, UINT id)
{
	switch (id)
	{
	case IDC_BUTTON1:
		if (SelectFolder(hwnd, m_dirPath))
		{
			SetDlgItemTextA(hwnd, IDC_EDIT1, m_dirPath);
		}
		break;
	case IDC_BUTTON2:
		if (SaveFileBox(hwnd, "输出文件", "所有文件(*.*)\0*.*\0\0", m_isoPath))
		{
			SetDlgItemTextA(hwnd, IDC_EDIT2, m_isoPath);
		}
		break;
	case IDC_BUTTON3:
		if (CheckParams(hwnd))
		{
			CreateISO(hwnd);
		}
		break;
	default:
		break;
	}
}

INT_PTR CALLBACK BoxProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		OnInitDialog(hDlg);
		return (INT_PTR)TRUE;
	case WM_COMMAND:
		OnCommand(hDlg, LOWORD(wParam));
		break;
	case WM_CLOSE:
		EndDialog(hDlg, 0);
		return 0;
	}
	return (INT_PTR)FALSE;
}

int APIENTRY wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPWSTR lpCmdLine,int nCmdShow)
{
	GetModuleFileNameA(NULL, m_exePath, sizeof(m_exePath));
	PathRemoveFileSpec(m_exePath);
	PathCombineA(m_exePath, m_exePath, "oscdimg.exe");
	if (!PathFileExistsA(m_exePath))
	{
		MessageBoxA(NULL, "没有找到oscdimg.exe", "文件丢失", MB_ICONERROR);
		return 0;
	}
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), NULL, BoxProc);
    return 0;
}
